#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
#
# This file is part of Coruja-scripts
#
# Coruja-scripts is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# Coruja-scripts is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Coruja-scripts.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2011 Grupo Falabrasil - http://www.laps.ufpa.br/falabrasil
#
# Author 2011: Pedro Batista - pedosb@gmail.com

import numpy as np

def autocorrelation(s, w=None, t=None, tau_range=None):
	"""
	As defined by Rabiner and Shafer, 1978
	Eq. (4.30)
	"""
	r = np.zeros(tau_range[1] - tau_range[0])
	for tau in xrange(*tau_range):
		if t+tau+len(w) > len(s):
			return r
		s1 = s[t:t+len(w)] * w
		s2 = s[t+tau:t+tau+len(w)] * w
		r[tau] = sum(s1 * s2)
	return r
